/**
 * Write By HQ_punan 2012.01.22
 * This Class is use for write string to xml
 */


package com.huaqin.punan;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;

public class WriteXMLFiles {
	public static final String TAG = "WriteXMLFiles";
	public static boolean debug = true;
	public static final String FILE_PATH = MainActivity.FILE_PATH;
	public static final String FILE_NAME = MainActivity.FILE_NAME;
	
	static boolean WriteXML(String strXML){
		
		if(debug) Log.d(TAG, "write xml to storage");
		File file = new File(FILE_PATH+FILE_NAME);
		
		try{
			if(!file.exists()){
				file.createNewFile();
			}
			
			OutputStream out = new FileOutputStream(file);
			out.write(strXML.getBytes());
			out.close();
			
		}catch(IOException e){
			e.printStackTrace();
			if(debug) Log.d(TAG, "Write XML Fail");
			return false;
		}
		
		return true;
		
	}
	
	
	static String generateXML(String sendFlag, String outGoing, String inComing){
		
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		if(debug) Log.d(TAG, "Generate XML File");
		
		try{
			serializer.setOutput(writer);
			
			serializer.startDocument("UTF-8", true);
			serializer.startTag("","salesinfo");
			serializer.startTag("","ngm");
			
			serializer.startTag("", "send_flag");
			serializer.text(sendFlag);
			serializer.endTag("", "send_flag");
			
			serializer.startTag("", "out_going");
			serializer.text(outGoing);
			serializer.endTag("", "out_going");
			
			serializer.startTag("", "in_coming");
			serializer.text(inComing);
			serializer.endTag("", "in_coming");
			
			serializer.endTag("","ngm");
			serializer.endTag("", "salesinfo");
			serializer.endDocument();
			
			return writer.toString();
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
}
